#!/usr/bin/env python
from lib.format_time import *
from lib.HandleMysql import *
from lib.switch_code import *
from shop_conn import *
import sys
import re
reload(sys)
sys.setdefaultencoding('utf-8')

def get_user_action_data(date):
    
    print "get_user_action_data begin : %s"%date
    
    stat_conn = set_stats_conn()
    shop_conn = set_shop_conn()
    stat_table = "user_action"
    
    query = "select shop_id,time_on_site,avg_time_on_site,order_count,pay_order_count,pv,uv,pay_amount from shop_flux_day where stat_date='%s' "%date
    items = stat_conn.get_results(query)
    for item in items:
        shop_id = item[0]
        time_on_site = item[1]
        avg_time_on_site = item[2]
        order_count = item[3]
        pay_order_count = item[4]
        pv = item[5]
        uv=item[6]
        pay_amount = item[7]
        
        query_in = "select userid,shopname,cateid,local,date(addTime) from shop where id=%s"%shop_id
        items_tmp = shop_conn.get_results(query_in)
        if len(items_tmp) == 0:
            continue
        user_id =  items_tmp[0][0]
        shop_name =  items_tmp[0][1]
        cate_id =  items_tmp[0][2]
        local = items_tmp[0][3]
        add_date = items_tmp[0][4]
        
        p_cityid = 0
        if local != None:            
            pattern =  re.compile(r'"L":(\w*)')
            matchs = pattern.search(local)
            try:
                p_cityid = matchs.group(1)
            except Exception,e:
                p_cityid = 0
        p_city_name = ''
        if p_cityid > 0:
            query_tmp = "select localname from t_local where localid=%s"%p_cityid
            items_tmp = stat_conn.get_results(query_tmp)
            try:
                p_city_name =  items_tmp[0][0]
            except Exception,e:
                p_city_name = ''
        
        #user type
        user_type=0
        if date == add_date:
            user_type =1
        
        order_rate = 0
        switch_rate = 0
        if pay_order_count > 0:
            order_rate = pv/pay_order_count 
            switch_rate = uv/pay_order_count
        
        #comment
        query_tmp = "select type,count(1) from comment where type>0 and commenttype=2 and shopid=%s group by type order by type desc"%shop_id
        items_tmp = shop_conn.get_results(query_tmp)
        
        good_count=0;
        mid_count = 0
        bad_count =0
        comment_rate = 0
        
        for item in items_tmp:
            tp = item[0]
            cnt = item[1]
            if tp == 1:
                good_count = cnt
            elif tp == 2:
                mid_count=cnt
            else:
                bad_count=cnt
        tl_count = good_count + mid_count + bad_count
        if good_count > 0:
            comment_rate = good_count/tl_count
        
        param = [date,user_id,shop_id,shop_name,p_cityid,p_city_name,time_on_site,avg_time_on_site,user_type,0,pay_order_count,pay_amount,0,0,0,pv,0,order_rate,switch_rate,good_count,mid_count,bad_count,comment_rate]
        stat_conn.insert_record(stat_table,param)
    
    stat_conn.close()
    shop_conn.close()
    print "get_user_action_data end : %s"%date

#logic
def execute(date):
    # get main data
    # from order orderproduct orderticket orderpay orderlog
    get_user_action_data(date)
    pass
def main():

   #date = '2012-05-28'
   #execute(date)   
   if len(sys.argv)==1:
        start_date=get_yesterday()
        end_date=start_date
   elif len(sys.argv)==2:
        start_date=format_date(sys.argv[1])
        end_date=start_date
   elif len(sys.argv)==3:
        start_date=format_date(sys.argv[1])
        end_date=format_date(sys.argv[2])
   stat_date=start_date
   while stat_date>=start_date and stat_date<=end_date:
        print "The program of the date of %s is beginning at %s " %(stat_date,get_now())
        #
        execute( stat_date) 
        print "The program of the date of %s is end at %s " %(stat_date,get_now())
        stat_date=var_date(stat_date)


if __name__=='__main__':
    main()

